package com.tshopping.controller;

import com.tshopping.service.UserService;
import com.tshopping.util.EmailSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;

/**
 * @name: ForgetController
 * @description: 忘记密码页面控制器
 * @author: Jackeys
 * @date: 2020-12-17 18:35
 */
@RestController
@RequestMapping("/ForgetController")
@CrossOrigin
public class ForgetController {
    @Autowired
    private UserService userService;

    private String code=null;

    @PostMapping(value = "/forget")
    /**
     * @description 找回密码
     * @Param: email
     * @Param: password
     * @Param: securityCode
     * @return boolean
     * @author Jackeys
     * @date 2020-12-17 18:47
     */
    public boolean register(String email, String password,String securityCode){
        if(securityCode.equals(code)){
            return userService.forgetPassword(email,password);
        }
        return false;
    }

    @PostMapping(value = "/sendSecurityCode")
    /**
     * @description 发送验证码
     * @Param: email
     * @return boolean
     * @author Jackeys
     * @date 2020-12-17 18:47
     */
    public boolean sendEmail(String email) throws MessagingException {
        String securityCode= EmailSender.getSecurityCode(6);
        if(EmailSender.sendEmail(email,securityCode)){
            code=securityCode;
            return true;
        }
        return false;
    }
}
